package com.lyy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.lyy.entity.Episode;
import com.lyy.mapper.EpisodeMapper;
import com.lyy.service.EpisodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Liangyaoyun
 */
@Service
@CacheConfig(cacheNames = "lesson:cache:episode")
public class EpisodeServiceImpl implements EpisodeService {
    @Autowired
    private EpisodeMapper episodeMapper;

    @Cacheable(key = "#episodeId", condition = "#episodeId != null")
    @Override
    public Episode selectByEpisodeId(Integer episodeId) {

        // DB: 按视频集主键查询一条集记录
        Episode condition = new Episode();
        condition.setEpisodeId(episodeId);
        List<Episode> episodes = episodeMapper.select(condition);
        if (ObjectUtil.isEmpty(episodes)) {
            throw new RuntimeException("集查询失败: 集主键不存在");
        }

        return episodes.get(0);
    }
}
